NETWORK ANALYSIS OF SHAKESPEARE'S JULIUS CAESAR

In [8]:
%matplotlib inline 
%load_ext autoreload
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

Text Preparation

The text file of Shakespeare's Julius Caesar ('corpora/ShakespeareJuliusCaesarOrig_from_PG.txt') was downloaded from Project Gutenberg and it was modified in such a way that all the names of characters (as "actors" in the social networks sense) appear as such in a uniform way. This modified text ('corpora/ShakespeareJuliusCaesar_mod1.txt') is the one processed here.

In [9]:
fileName = 'corpora/ShakespeareJuliusCaesar_mod1.txt'
f=open(fileName,'r')
In [10]:
%autoreload 2
from testy import *
from syntheticThreeLayerGraph_time import synthetic_multi_level_dict,plot_graph_dict,plot_graph_k_n, plot_total_graph_with_weights

act_dict,u,pers_l,pers_dict,pact,lact=create_dict_of_acts(fileName)

print 'Characters (actors) appearing in Julius Caesar:'
print
for actor in pers_l:
    print actor
print
print 'The number of characters (actors) in Julius Caesar is', len(pers_l)
Characters (actors) appearing in Julius Caesar:

ANTONIUS
ARTEMIDORUS
BRUTUS
CAESAR
CALPURNIA
CASCA
CASSIUS
CATO
CICERO
CINNA
CINNA THE POET
CITIZENS
CLAUDIUS
CLITUS
DARDANIUS
DECIUS
FIRST CITIZEN
FIRST SERVANT
FIRST SOLDIER
FLAVIUS
FOURTH CITIZEN
GHOST
LEPIDUS
LIGARIUS
LUCILIUS
LUCIUS
MARULLUS
MESSALA
MESSENGER
METELLUS
OCTAVIUS
PINDARUS
POPILIUS
PORTIA
PUBLIUS
SECOND CITIZEN
SECOND SERVANT
SECOND SOLDIER
SOOTHSAYER
STRATO
THIRD CITIZEN
THIRD SERVANT
THIRD SOLDIER
TITINIUS
TREBONIUS
VARRO
VOLUMNIUS

The number of characters (actors) in Julius Caesar is 47

Detection of characters (actors) and conversational relationships (ties) among them

The relationship (tie) among the above characters (actors) detected (automatically) here is the so-called "conversational relationship" which is defined whenever two characters are co-participating in a conversation. Actually, due to the structure of Shakespeare's printed text (in Project Gutenberg), the unit of conversation (or conversational chunk) used here is the body of the text which is delimited between two empty lines. In this way, the resulting network is represented by a weighted undirected graph, where tha weight of a edge joining two characters is the total number of conversational chaunks that these characters are involved in. Moreover, we are decomposing the network into slices (or layers) according to one of the five Acts during which the measured coversations take place.

In [11]:
graph_dic,ract_dic,pernode_dict,nodper_dic,cnum=create_graph_dict(act_dict,pers_l,pers_dict,u)
G, list_of_Graphs_final, Gagr, edgeList ,nmap ,mapping,k,n=synthetic_multi_level_dict(graph_dic,pernode_dict,nodper_dic,ract_dic,No_isolates=True)
conver_rel = 0
pos_dict={}
for k,v in graph_dic.items():
    print nx.info(v)
    conver_rel += len(v.edges())
    print
    
print 'Characters (actors) appearing in Julius Caesar in all conversational relationships in all Acts:'  
print
for i in pernode_dict:
    print i
print
print 'The total number of characters (actors) appearing in Julius Caesar in all conversational relationships in all Acts is', len(pernode_dict.keys())
print 
print 'The total number of conversational relationships (edges) among characters (actors) \
taking place in all Acts of Julius Caesar is', conver_rel
Name: Act I
Type: Graph
Number of nodes: 47
Number of edges: 30
Average degree:   1.2766

Name: Act II
Type: Graph
Number of nodes: 47
Number of edges: 42
Average degree:   1.7872

Name: Act IV
Type: Graph
Number of nodes: 47
Number of edges: 34
Average degree:   1.4468

Name: Act III
Type: Graph
Number of nodes: 47
Number of edges: 64
Average degree:   2.7234

Name: Act V
Type: Graph
Number of nodes: 47
Number of edges: 42
Average degree:   1.7872

Characters (actors) appearing in Julius Caesar in all conversational relationships in all Acts:

PORTIA
CLAUDIUS
FIRST CITIZEN
MARULLUS
FIRST SOLDIER
SOOTHSAYER
CINNA THE POET
ANTONIUS
GHOST
VARRO
MESSENGER
ARTEMIDORUS
DECIUS
LUCIUS
BRUTUS
CASCA
TITINIUS
LIGARIUS
CICERO
MESSALA
PINDARUS
CAESAR
LUCILIUS
DARDANIUS
CLITUS
STRATO
THIRD SERVANT
POPILIUS
CASSIUS
CALPURNIA
LEPIDUS
PUBLIUS
FLAVIUS
OCTAVIUS
FIRST SERVANT
THIRD CITIZEN
METELLUS
SECOND SOLDIER
CINNA
SECOND CITIZEN
SECOND SERVANT
FOURTH CITIZEN
CITIZENS
TREBONIUS
CATO
VOLUMNIUS
THIRD SOLDIER

The total number of characters (actors) appearing in Julius Caesar in all conversational relationships in all Acts is 47

The total number of conversational relationships (edges) among characters (actors) taking place in all Acts of Julius Caesar is 212

Julius Caesar Network Visualizations

In [49]:
GI = graph_dic[ract_dic[cnum[0]]]
print "The number of actors in Julius Caesar's Act I is", len(GI.nodes())
print "The number of conversational relationships in Julius Caesar's Act I is", len(GI.edges())

GI.remove_nodes_from(nx.isolates(GI))
labels={i:v for v,i in pernode_dict.items() if i in GI.nodes()}
weights={(i[0],i[1]):i[2]['weight'] for i in GI.edges(data=True) }#if all((i[0],i[1])) in G.nodes() }
plt.figure(figsize=(12,12))
pos=nx.spring_layout(GI,scale=50)
pos_dict[0]=pos
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(GI,pos=pos,with_labels=False,alpha=0.4)
labe=nx.draw_networkx_labels(GI,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(GI,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=0.2)
plt.title("Julius Caesar Act I",fontsize=20)
kk=plt.axis('off')
The number of actors in Julius Caesar's Act I is 16
The number of conversational relationships in Julius Caesar's Act I is 30
In [13]:
GI = graph_dic[ract_dic[cnum[1]]]
print "The number of actors in Julius Caesar's Act II is", len(GI.nodes())
print "The number of conversational relationships in Julius Caesar's Act II is", len(GI.edges())

GI.remove_nodes_from(nx.isolates(GI))
labels={i:v for v,i in pernode_dict.items() if i in GI.nodes()}
weights={(i[0],i[1]):i[2]['weight'] for i in GI.edges(data=True) }#if all((i[0],i[1])) in G.nodes() }
plt.figure(figsize=(12,12))
pos=nx.spring_layout(GI,scale=50)
pos_dict[1]=pos
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(GI,pos=pos,with_labels=False,alpha=0.4)
labe=nx.draw_networkx_labels(GI,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(GI,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=0.2)
plt.title("Julius Caesar Act II",fontsize=20)
kk=plt.axis('off')
The number of actors in Julius Caesar's Act II is 47
The number of conversational relationships in Julius Caesar's Act II is 42
In [55]:
GI = graph_dic[ract_dic[cnum[2]]]
print "The number of actors in Julius Caesar's Act III is", len(GI.nodes())
print "The number of conversational relationships in Julius Caesar's Act III is", len(GI.edges())

GI.remove_nodes_from(nx.isolates(GI))
labels={i:v for v,i in pernode_dict.items() if i in GI.nodes()}
weights={(i[0],i[1]):i[2]['weight'] for i in GI.edges(data=True) }#if all((i[0],i[1])) in G.nodes() }
plt.figure(figsize=(12,12))
pos=nx.spring_layout(GI,scale=50)
pos_dict[2]=pos
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(GI,pos=pos,with_labels=False,alpha=0.4)
labe=nx.draw_networkx_labels(GI,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(GI,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=0.2)
plt.title("Julius Caesar Act III",fontsize=20)
kk=plt.axis('off')
The number of actors in Julius Caesar's Act III is 24
The number of conversational relationships in Julius Caesar's Act III is 64
In [56]:
GI = graph_dic[ract_dic[cnum[3]]]
print "The number of actors in Julius Caesar's Act IV is", len(GI.nodes())
print "The number of conversational relationships in Julius Caesar's Act IV is", len(GI.edges())

GI.remove_nodes_from(nx.isolates(GI))
labels={i:v for v,i in pernode_dict.items() if i in GI.nodes()}
weights={(i[0],i[1]):i[2]['weight'] for i in GI.edges(data=True) }#if all((i[0],i[1])) in G.nodes() }
plt.figure(figsize=(12,12))
pos=nx.spring_layout(GI,scale=50)
pos_dict[3]=pos
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(GI,pos=pos,with_labels=False,alpha=0.4)
labe=nx.draw_networkx_labels(GI,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(GI,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=0.2)
plt.title("Julius Caesar Act IV",fontsize=20)
kk=plt.axis('off')
The number of actors in Julius Caesar's Act IV is 17
The number of conversational relationships in Julius Caesar's Act IV is 34
In [60]:
GI = graph_dic[ract_dic[cnum[4]]]
print "The number of actors in Julius Caesar's Act V is", len(GI.nodes())
print "The number of conversational relationships in Julius Caesar's Act V is", len(GI.edges())

GI.remove_nodes_from(nx.isolates(GI))
labels={i:v for v,i in pernode_dict.items() if i in GI.nodes()}
weights={(i[0],i[1]):i[2]['weight'] for i in GI.edges(data=True) }#if all((i[0],i[1])) in G.nodes() }
plt.figure(figsize=(12,12))
pos=nx.spring_layout(GI,scale=50)
pos_dict[4]=pos
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(GI,pos=pos,with_labels=False,alpha=0.4)
labe=nx.draw_networkx_labels(GI,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(GI,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=0.2)
plt.title("Julius Caesar Act V",fontsize=20)
kk=plt.axis('off')
The number of actors in Julius Caesar's Act V is 17
The number of conversational relationships in Julius Caesar's Act V is 42
In [64]:
G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
print "The number of actors in Julius Caesar Network (all Acts) is", len(G.nodes())
print "The number of conversational relationships in Julius Caesar Network (all Acts) is", len(G.edges())

# print labels
plt.figure(figsize=(12,12))
pos=nx.spring_layout(G,scale=50)
pos_dict[5]=pos
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Network (all Acts)",fontsize=20)
kk=plt.axis('off')
The number of actors in Julius Caesar Network (all Acts) is 43
The number of conversational relationships in Julius Caesar Network (all Acts) is 149

Julius Caesar Network Centralities

Degree Centrality

In [65]:
G = graph_dic[ract_dic[cnum[0]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
dc = nx.degree_centrality(G)
dcs={}
for k,v in dc.items():
    dcs[v]=k
for k in sorted(dcs,reverse=True):
    print labels[dcs[k]], 'has degree centrality', k
pos=pos_dict[0]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=dc.keys(),
                       node_size = [d*5000 for d in dc.values()],node_color=dc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act I Degree Centralities",fontsize=20)
kk=plt.axis('off')
CAESAR has degree centrality 0.733333333333
CASSIUS has degree centrality 0.666666666667
CASCA has degree centrality 0.6
CICERO has degree centrality 0.266666666667
ANTONIUS has degree centrality 0.2
CALPURNIA has degree centrality 0.133333333333
SOOTHSAYER has degree centrality 0.0666666666667
In [66]:
G = graph_dic[ract_dic[cnum[1]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
# dc = nx.degree_centrality(G)
# for k,v in dc.items():
#     print labels[k], 'has degree centrality', v
dc = nx.degree_centrality(G)
dcs={}
for k,v in dc.items():
    dcs[v]=k
for k in sorted(dcs,reverse=True):
    print labels[dcs[k]], 'has degree centrality', k
pos=pos_dict[1]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=dc.keys(),
                       node_size = [d*5000 for d in dc.values()],node_color=dc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act II Degree Centralities",fontsize=20)
kk=plt.axis('off')
CAESAR has degree centrality 0.9375
CASSIUS has degree centrality 0.6875
BRUTUS has degree centrality 0.5625
METELLUS has degree centrality 0.3125
LIGARIUS has degree centrality 0.25
CALPURNIA has degree centrality 0.1875
PUBLIUS has degree centrality 0.0625
In [67]:
G = graph_dic[ract_dic[cnum[2]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
dc = nx.degree_centrality(G)
dcs={}
for k,v in dc.items():
    dcs[v]=k
for k in sorted(dcs,reverse=True):
    print labels[dcs[k]], 'has degree centrality', k
pos=pos_dict[2]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=dc.keys(),
                       node_size = [d*5000 for d in dc.values()],node_color=dc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act III Degree Centralities",fontsize=20)
kk=plt.axis('off')
ANTONIUS has degree centrality 0.782608695652
BRUTUS has degree centrality 0.608695652174
CASSIUS has degree centrality 0.565217391304
CINNA has degree centrality 0.304347826087
THIRD CITIZEN has degree centrality 0.260869565217
METELLUS has degree centrality 0.217391304348
SECOND CITIZEN has degree centrality 0.173913043478
POPILIUS has degree centrality 0.130434782609
OCTAVIUS has degree centrality 0.0869565217391
SOOTHSAYER has degree centrality 0.0434782608696
In [68]:
G = graph_dic[ract_dic[cnum[3]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
dc = nx.degree_centrality(G)
dcs={}
for k,v in dc.items():
    dcs[v]=k
for k in sorted(dcs,reverse=True):
    print labels[dcs[k]], 'has degree centrality', k
pos=pos_dict[3]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=dc.keys(),
                       node_size = [d*5000 for d in dc.values()],node_color=dc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act IV Degree Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has degree centrality 0.875
CASSIUS has degree centrality 0.6875
MESSALA has degree centrality 0.375
OCTAVIUS has degree centrality 0.3125
LEPIDUS has degree centrality 0.25
LUCILIUS has degree centrality 0.1875
CAESAR has degree centrality 0.125
PUBLIUS has degree centrality 0.0625
In [69]:
G = graph_dic[ract_dic[cnum[4]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
dc = nx.degree_centrality(G)
dcs={}
for k,v in dc.items():
    dcs[v]=k
for k in sorted(dcs,reverse=True):
    print labels[dcs[k]], 'has degree centrality', k
pos=pos_dict[4]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=dc.keys(),
                       node_size = [d*5000 for d in dc.values()],node_color=dc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act V Degree Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has degree centrality 1.0
ANTONIUS has degree centrality 0.625
CASSIUS has degree centrality 0.5
LUCILIUS has degree centrality 0.375
PINDARUS has degree centrality 0.3125
CAESAR has degree centrality 0.1875
STRATO has degree centrality 0.125
VOLUMNIUS has degree centrality 0.0625
In [70]:
G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
# G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
dc = nx.degree_centrality(G)
dcs={}
for k,v in dc.items():
    dcs[v]=k
for k in sorted(dcs,reverse=True):
    print labels[dcs[k]], 'has degree centrality', k
pos=pos_dict[5]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=dc.keys(),
                       node_size = [d*2000 for d in dc.values()],node_color=dc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Network (all Acts) Degree Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has degree centrality 0.857142857143
CAESAR has degree centrality 0.642857142857
ANTONIUS has degree centrality 0.595238095238
CASCA has degree centrality 0.285714285714
ARTEMIDORUS has degree centrality 0.261904761905
METELLUS has degree centrality 0.238095238095
OCTAVIUS has degree centrality 0.190476190476
CATO has degree centrality 0.166666666667
THIRD CITIZEN has degree centrality 0.142857142857
TREBONIUS has degree centrality 0.119047619048
SECOND CITIZEN has degree centrality 0.0952380952381
POPILIUS has degree centrality 0.0714285714286
SECOND SOLDIER has degree centrality 0.047619047619
SOOTHSAYER has degree centrality 0.0238095238095

Closeness Centrality

In [71]:
G = graph_dic[ract_dic[cnum[0]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
cl = nx.closeness_centrality(G)
cls={}
for k,v in cl.items():
    cls[v]=k
for k in sorted(cls,reverse=True):
    print labels[cls[k]], 'has closeness centrality', k
pos=pos_dict[0]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=cl.keys(),
                       node_size = [d*5000 for d in cl.values()],node_color=cl.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act I Closeness Centralities",fontsize=20)
kk=plt.axis('off')
CASSIUS has closeness centrality 0.681818181818
BRUTUS has closeness centrality 0.652173913043
CASCA has closeness centrality 0.625
CICERO has closeness centrality 0.555555555556
ANTONIUS has closeness centrality 0.483870967742
CALPURNIA has closeness centrality 0.454545454545
FLAVIUS has closeness centrality 0.441176470588
SOOTHSAYER has closeness centrality 0.416666666667
MARULLUS has closeness centrality 0.394736842105
METELLUS has closeness centrality 0.333333333333
In [72]:
G = graph_dic[ract_dic[cnum[1]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
cl = nx.closeness_centrality(G)
cls={}
for k,v in cl.items():
    cls[v]=k
for k in sorted(cls,reverse=True):
    print labels[cls[k]], 'has closeness centrality', k
pos=pos_dict[1]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=cl.keys(),
                       node_size = [d*5000 for d in cl.values()],node_color=cl.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act II Closeness Centralities",fontsize=20)
kk=plt.axis('off')
CAESAR has closeness centrality 0.842105263158
CASSIUS has closeness centrality 0.727272727273
ARTEMIDORUS has closeness centrality 0.666666666667
BRUTUS has closeness centrality 0.64
METELLUS has closeness centrality 0.571428571429
DECIUS has closeness centrality 0.551724137931
CASCA has closeness centrality 0.533333333333
LIGARIUS has closeness centrality 0.516129032258
CALPURNIA has closeness centrality 0.5
PUBLIUS has closeness centrality 0.470588235294
LUCIUS has closeness centrality 0.432432432432
CATO has closeness centrality 0.347826086957
In [73]:
G = graph_dic[ract_dic[cnum[2]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
cl = nx.closeness_centrality(G)
cls={}
for k,v in cl.items():
    cls[v]=k
for k in sorted(cls,reverse=True):
    print labels[cls[k]], 'has closeness centrality', k
pos=pos_dict[2]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=cl.keys(),
                       node_size = [d*5000 for d in cl.values()],node_color=cl.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act III Closeness Centralities",fontsize=20)
kk=plt.axis('off')
ANTONIUS has closeness centrality 0.766666666667
BRUTUS has closeness centrality 0.657142857143
CASSIUS has closeness centrality 0.638888888889
THIRD CITIZEN has closeness centrality 0.547619047619
CINNA has closeness centrality 0.522727272727
SECOND CITIZEN has closeness centrality 0.511111111111
CITIZENS has closeness centrality 0.5
POPILIUS has closeness centrality 0.489361702128
TREBONIUS has closeness centrality 0.479166666667
OCTAVIUS has closeness centrality 0.46
SOOTHSAYER has closeness centrality 0.442307692308
PUBLIUS has closeness centrality 0.425925925926
LEPIDUS has closeness centrality 0.359375
CINNA THE POET has closeness centrality 0.348484848485
In [74]:
G = graph_dic[ract_dic[cnum[3]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
cl = nx.closeness_centrality(G)
for k,v in cl.items():
    print labels[k], 'has closeness centrality', v
# pos=nx.spring_layout(G,scale=50)
pos=pos_dict[3]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=cl.keys(),
                       node_size = [d*5000 for d in cl.values()],node_color=cl.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act IV Closeness Centralities",fontsize=20)
kk=plt.axis('off')
PORTIA has closeness centrality 0.470588235294
CLAUDIUS has closeness centrality 0.470588235294
OCTAVIUS has closeness centrality 0.571428571429
ANTONIUS has closeness centrality 0.592592592593
VARRO has closeness centrality 0.470588235294
MESSALA has closeness centrality 0.592592592593
BRUTUS has closeness centrality 0.761904761905
TITINIUS has closeness centrality 0.444444444444
CICERO has closeness centrality 0.516129032258
LUCIUS has closeness centrality 0.340425531915
PINDARUS has closeness centrality 0.484848484848
CAESAR has closeness centrality 0.432432432432
LUCILIUS has closeness centrality 0.484848484848
GHOST has closeness centrality 0.444444444444
CASSIUS has closeness centrality 0.64
LEPIDUS has closeness centrality 0.432432432432
PUBLIUS has closeness centrality 0.307692307692
In [75]:
G = graph_dic[ract_dic[cnum[4]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
cl = nx.closeness_centrality(G)
cls={}
for k,v in cl.items():
    cls[v]=k
for k in sorted(cls,reverse=True):
    print labels[cls[k]], 'has closeness centrality', k
pos=pos_dict[4]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=cl.keys(),
                       node_size = [d*5000 for d in cl.values()],node_color=cl.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act V Closeness Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has closeness centrality 0.888888888889
ANTONIUS has closeness centrality 0.727272727273
TITINIUS has closeness centrality 0.666666666667
CASSIUS has closeness centrality 0.615384615385
OCTAVIUS has closeness centrality 0.592592592593
LUCILIUS has closeness centrality 0.571428571429
CAESAR has closeness centrality 0.551724137931
SECOND SOLDIER has closeness centrality 0.533333333333
STRATO has closeness centrality 0.5
VOLUMNIUS has closeness centrality 0.484848484848
PINDARUS has closeness centrality 0.470588235294
CASCA has closeness centrality 0.432432432432
In [76]:
G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
# G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
cl = nx.closeness_centrality(G)
cls={}
for k,v in cl.items():
    cls[v]=k
for k in sorted(cls,reverse=True):
    print labels[cls[k]], 'has closeness centrality', k
pos=pos_dict[5]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=cl.keys(),
                       node_size = [d*2000 for d in cl.values()],node_color=cl.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Network (all Acts) Closeness Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has closeness centrality 0.84
CAESAR has closeness centrality 0.71186440678
ANTONIUS has closeness centrality 0.688524590164
CASCA has closeness centrality 0.567567567568
CINNA has closeness centrality 0.56
ARTEMIDORUS has closeness centrality 0.552631578947
METELLUS has closeness centrality 0.545454545455
OCTAVIUS has closeness centrality 0.538461538462
THIRD CITIZEN has closeness centrality 0.53164556962
CICERO has closeness centrality 0.525
LUCILIUS has closeness centrality 0.518518518519
SECOND CITIZEN has closeness centrality 0.512195121951
CALPURNIA has closeness centrality 0.506024096386
POPILIUS has closeness centrality 0.5
SECOND SOLDIER has closeness centrality 0.488372093023
TREBONIUS has closeness centrality 0.477272727273
STRATO has closeness centrality 0.47191011236
DARDANIUS has closeness centrality 0.466666666667
VOLUMNIUS has closeness centrality 0.461538461538
THIRD SERVANT has closeness centrality 0.451612903226
LEPIDUS has closeness centrality 0.442105263158
LUCIUS has closeness centrality 0.428571428571
SOOTHSAYER has closeness centrality 0.42
MARULLUS has closeness centrality 0.365217391304
CINNA THE POET has closeness centrality 0.362068965517

Betweenness Centrality

In [77]:
G = graph_dic[ract_dic[cnum[0]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
bc = nx.betweenness_centrality(G)
bcs={}
for k,v in bc.items():
    bcs[v]=k
for k in sorted(bcs,reverse=True):
    print labels[bcs[k]], 'has betweenness centrality', k
pos=pos_dict[0]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=bc.keys(),
                       node_size = [d*5000 for d in bc.values()],node_color=bc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act I Betweenness Centralities",fontsize=20)
kk=plt.axis('off')
CASSIUS has betweenness centrality 0.438095238095
CAESAR has betweenness centrality 0.363492063492
CASCA has betweenness centrality 0.192063492063
BRUTUS has betweenness centrality 0.15873015873
CINNA has betweenness centrality 0.133333333333
CALPURNIA has betweenness centrality 0.0
In [78]:
G = graph_dic[ract_dic[cnum[1]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
bc = nx.betweenness_centrality(G)
bcs={}
for k,v in bc.items():
    bcs[v]=k
for k in sorted(bcs,reverse=True):
    print labels[bcs[k]], 'has betweenness centrality', k
pos=pos_dict[1]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=bc.keys(),
                       node_size = [d*5000 for d in bc.values()],node_color=bc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act II Betweenness Centralities",fontsize=20)
kk=plt.axis('off')
CAESAR has betweenness centrality 0.411805555556
CASSIUS has betweenness centrality 0.301388888889
PORTIA has betweenness centrality 0.125
BRUTUS has betweenness centrality 0.0743055555556
ARTEMIDORUS has betweenness centrality 0.0618055555556
DECIUS has betweenness centrality 0.0125
METELLUS has betweenness centrality 0.00625
CALPURNIA has betweenness centrality 0.00277777777778
PUBLIUS has betweenness centrality 0.0
In [79]:
G = graph_dic[ract_dic[cnum[2]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
bc = nx.betweenness_centrality(G)
bcs={}
for k,v in bc.items():
    bcs[v]=k
for k in sorted(bcs,reverse=True):
    print labels[bcs[k]], 'has betweenness centrality', k
pos=pos_dict[2]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=bc.keys(),
                       node_size = [d*5000 for d in bc.values()],node_color=bc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act III Betweenness Centralities",fontsize=20)
kk=plt.axis('off')
CAESAR has betweenness centrality 0.342443064182
ANTONIUS has betweenness centrality 0.269715791455
BRUTUS has betweenness centrality 0.107942781856
SECOND SERVANT has betweenness centrality 0.100790513834
CASSIUS has betweenness centrality 0.0983813288161
CINNA has betweenness centrality 0.0906455862978
THIRD CITIZEN has betweenness centrality 0.0893280632411
CASCA has betweenness centrality 0.0121400338792
DECIUS has betweenness centrality 0.00803689064559
FOURTH CITIZEN has betweenness centrality 0.00310559006211
SOOTHSAYER has betweenness centrality 0.0
In [80]:
G = graph_dic[ract_dic[cnum[3]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
bc = nx.betweenness_centrality(G)
bcs={}
for k,v in bc.items():
    bcs[v]=k
for k in sorted(bcs,reverse=True):
    print labels[bcs[k]], 'has betweenness centrality', k
pos=pos_dict[3]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=bc.keys(),
                       node_size = [d*5000 for d in bc.values()],node_color=bc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act IV Betweenness Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has betweenness centrality 0.609722222222
CASSIUS has betweenness centrality 0.155555555556
LEPIDUS has betweenness centrality 0.125
ANTONIUS has betweenness centrality 0.105555555556
MESSALA has betweenness centrality 0.0777777777778
VARRO has betweenness centrality 0.0583333333333
OCTAVIUS has betweenness centrality 0.0555555555556
LUCIUS has betweenness centrality 0.00416666666667
PUBLIUS has betweenness centrality 0.0
In [81]:
G = graph_dic[ract_dic[cnum[4]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
bc = nx.betweenness_centrality(G)
bcs={}
for k,v in bc.items():
    bcs[v]=k
for k in sorted(bcs,reverse=True):
    print labels[bcs[k]], 'has betweenness centrality', k
pos=pos_dict[4]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=bc.keys(),
                       node_size = [d*5000 for d in bc.values()],node_color=bc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act V Betweenness Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has betweenness centrality 0.576388888889
ANTONIUS has betweenness centrality 0.218055555556
TITINIUS has betweenness centrality 0.0527777777778
CASSIUS has betweenness centrality 0.0305555555556
MESSALA has betweenness centrality 0.0270833333333
OCTAVIUS has betweenness centrality 0.00555555555556
LUCILIUS has betweenness centrality 0.00416666666667
CATO has betweenness centrality 0.00208333333333
VOLUMNIUS has betweenness centrality 0.0
In [82]:
G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
# G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
bc = nx.betweenness_centrality(G)
bcs={}
for k,v in bc.items():
    bcs[v]=k
for k in sorted(bcs,reverse=True):
    print labels[bcs[k]], 'has betweenness centrality', k
pos=pos_dict[5]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=bc.keys(),
                       node_size = [d*2000 for d in bc.values()],node_color=bc.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Network (all Acts) Betweenness Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has betweenness centrality 0.456979523828
CAESAR has betweenness centrality 0.166031050067
ANTONIUS has betweenness centrality 0.12743645091
CASSIUS has betweenness centrality 0.122626033725
CASCA has betweenness centrality 0.0529744075636
CINNA has betweenness centrality 0.0499546630805
MESSALA has betweenness centrality 0.0108937347063
ARTEMIDORUS has betweenness centrality 0.0089085227587
OCTAVIUS has betweenness centrality 0.0059998877989
PUBLIUS has betweenness centrality 0.00396183680614
METELLUS has betweenness centrality 0.00387779182938
VARRO has betweenness centrality 0.00376638460262
TITINIUS has betweenness centrality 0.00293125380233
LUCIUS has betweenness centrality 0.00253857640617
DECIUS has betweenness centrality 0.00198718340567
LEPIDUS has betweenness centrality 0.00149051490515
CALPURNIA has betweenness centrality 0.00144488689785
THIRD CITIZEN has betweenness centrality 0.00137487367453
CICERO has betweenness centrality 0.00104529616725
PORTIA has betweenness centrality 0.000940213483767
LUCILIUS has betweenness centrality 0.000829600132736
CATO has betweenness centrality 0.00077429345722
LIGARIUS has betweenness centrality 0.000464576074332
TREBONIUS has betweenness centrality 0.000206308454062
SOOTHSAYER has betweenness centrality 0.0

Eigenvector Centrality

In [83]:
G = graph_dic[ract_dic[cnum[0]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
ec = nx.eigenvector_centrality(G)
ecs={}
for k,v in ec.items():
    ecs[v]=k
for k in sorted(ecs,reverse=True):
    print labels[ecs[k]], 'has eigenvector centrality', k
pos=pos_dict[0]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=ec.keys(),
                       node_size = [d*5000 for d in ec.values()],node_color=ec.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act I Eigenvector Centralities",fontsize=20)
kk=plt.axis('off')
CASSIUS has eigenvector centrality 0.638023856184
BRUTUS has eigenvector centrality 0.483062086883
CAESAR has eigenvector centrality 0.449788215705
CASCA has eigenvector centrality 0.347081341517
ANTONIUS has eigenvector centrality 0.139629912226
CICERO has eigenvector centrality 0.0854205691361
CINNA has eigenvector centrality 0.0762452741094
CALPURNIA has eigenvector centrality 0.0459158441718
FLAVIUS has eigenvector centrality 0.0312380226847
TITINIUS has eigenvector centrality 0.0159873799957
SOOTHSAYER has eigenvector centrality 0.0112705273078
MARULLUS has eigenvector centrality 0.00869696806908
METELLUS has eigenvector centrality 0.00382100852807
In [84]:
G = graph_dic[ract_dic[cnum[1]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
ec = nx.eigenvector_centrality(G)
ecs={}
for k,v in ec.items():
    ecs[v]=k
for k in sorted(ecs,reverse=True):
    print labels[ecs[k]], 'has eigenvector centrality', k
pos=pos_dict[1]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=ec.keys(),
                       node_size = [d*5000 for d in ec.values()],node_color=ec.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act II Eigenvector Centralities",fontsize=20)
kk=plt.axis('off')
CAESAR has eigenvector centrality 0.671205888956
BRUTUS has eigenvector centrality 0.521601998489
PORTIA has eigenvector centrality 0.343942332449
ARTEMIDORUS has eigenvector centrality 0.204626449899
CASSIUS has eigenvector centrality 0.187756318775
DECIUS has eigenvector centrality 0.177991150609
CALPURNIA has eigenvector centrality 0.129680099379
LIGARIUS has eigenvector centrality 0.107883239595
ANTONIUS has eigenvector centrality 0.0873329835002
METELLUS has eigenvector centrality 0.0871841233888
TREBONIUS has eigenvector centrality 0.0524089276902
PUBLIUS has eigenvector centrality 0.0405548327171
CASCA has eigenvector centrality 0.0321315113316
CATO has eigenvector centrality 0.0103905700366
LUCIUS has eigenvector centrality 0.00567220131594
In [85]:
G = graph_dic[ract_dic[cnum[2]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
ec = nx.eigenvector_centrality(G)
ecs={}
for k,v in ec.items():
    ecs[v]=k
for k in sorted(ecs,reverse=True):
    print labels[ecs[k]], 'has eigenvector centrality', k
pos=pos_dict[2]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=ec.keys(),
                       node_size = [d*5000 for d in ec.values()],node_color=ec.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act III Eigenvector Centralities",fontsize=20)
kk=plt.axis('off')
ANTONIUS has eigenvector centrality 0.576087695099
CAESAR has eigenvector centrality 0.56665849873
BRUTUS has eigenvector centrality 0.523782947943
CASSIUS has eigenvector centrality 0.182349968188
SECOND SERVANT has eigenvector centrality 0.133379931719
THIRD CITIZEN has eigenvector centrality 0.0601954996528
FOURTH CITIZEN has eigenvector centrality 0.057582350326
SECOND CITIZEN has eigenvector centrality 0.0526402007779
METELLUS has eigenvector centrality 0.0431180541199
FIRST CITIZEN has eigenvector centrality 0.0409254894665
PUBLIUS has eigenvector centrality 0.0363899875723
CINNA has eigenvector centrality 0.0355071479559
ARTEMIDORUS has eigenvector centrality 0.0335315460957
CITIZENS has eigenvector centrality 0.0324028731694
CASCA has eigenvector centrality 0.0289073480013
DECIUS has eigenvector centrality 0.0287961865505
OCTAVIUS has eigenvector centrality 0.0275410423337
POPILIUS has eigenvector centrality 0.0265778803101
THIRD SERVANT has eigenvector centrality 0.016905502821
TREBONIUS has eigenvector centrality 0.0116462457565
SOOTHSAYER has eigenvector centrality 0.00838288652391
LEPIDUS has eigenvector centrality 0.00197319344236
CINNA THE POET has eigenvector centrality 0.00105057832292
LIGARIUS has eigenvector centrality 0.000890517633699
In [86]:
G = graph_dic[ract_dic[cnum[3]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
ec = nx.eigenvector_centrality(G,max_iter=1000)
ecs={}
for k,v in ec.items():
    ecs[v]=k
for k in sorted(ecs,reverse=True):
    print labels[ecs[k]], 'has eigenvector centrality', k
pos=pos_dict[3]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=ec.keys(),
                       node_size = [d*5000 for d in ec.values()],node_color=ec.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act IV Eigenvector Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has eigenvector centrality 0.681036861939
CASSIUS has eigenvector centrality 0.676486062503
MESSALA has eigenvector centrality 0.133558264686
LUCILIUS has eigenvector centrality 0.126748110634
ANTONIUS has eigenvector centrality 0.121525271319
TITINIUS has eigenvector centrality 0.0836584734567
PORTIA has eigenvector centrality 0.0833795925242
OCTAVIUS has eigenvector centrality 0.0821821566309
CICERO has eigenvector centrality 0.0457914246117
PINDARUS has eigenvector centrality 0.0455822837486
CAESAR has eigenvector centrality 0.0452824286526
CLAUDIUS has eigenvector centrality 0.0418885231698
VARRO has eigenvector centrality 0.0209739048057
GHOST has eigenvector centrality 0.0209146183642
LEPIDUS has eigenvector centrality 0.017838506928
LUCIUS has eigenvector centrality 0.00193053785214
PUBLIUS has eigenvector centrality 0.000547822227941
In [87]:
G = graph_dic[ract_dic[cnum[4]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
ec = nx.eigenvector_centrality(G)
ecs={}
for k,v in ec.items():
    ecs[v]=k
for k in sorted(ecs,reverse=True):
    print labels[ecs[k]], 'has eigenvector centrality', k
pos=pos_dict[4]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=ec.keys(),
                       node_size = [d*5000 for d in ec.values()],node_color=ec.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act V Eigenvector Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has eigenvector centrality 0.577352241577
CASSIUS has eigenvector centrality 0.383472431776
MESSALA has eigenvector centrality 0.33188557033
TITINIUS has eigenvector centrality 0.314900434083
ANTONIUS has eigenvector centrality 0.296276176395
OCTAVIUS has eigenvector centrality 0.245947681934
LUCILIUS has eigenvector centrality 0.229325002418
CAESAR has eigenvector centrality 0.197422396286
STRATO has eigenvector centrality 0.147278060044
PINDARUS has eigenvector centrality 0.127506283001
VOLUMNIUS has eigenvector centrality 0.116899488297
CATO has eigenvector centrality 0.101862951763
CLITUS has eigenvector centrality 0.0740731810974
SECOND SOLDIER has eigenvector centrality 0.0353775618848
DARDANIUS has eigenvector centrality 0.0323785687259
FLAVIUS has eigenvector centrality 0.0233798976594
CASCA has eigenvector centrality 0.0119976642254
In [88]:
G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
# G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
ec = nx.eigenvector_centrality(G)
ecs={}
for k,v in ec.items():
    ecs[v]=k
for k in sorted(ecs,reverse=True):
    print labels[ecs[k]], 'has eigenvector centrality', k
pos=pos_dict[5]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=ec.keys(),
                       node_size = [d*2000 for d in ec.values()],node_color=ec.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Network (all Acts) Eigenvector Centralities",fontsize=20)
kk=plt.axis('off')
BRUTUS has eigenvector centrality 0.56933558257
CAESAR has eigenvector centrality 0.50142697793
CASSIUS has eigenvector centrality 0.456244577027
ANTONIUS has eigenvector centrality 0.401104756143
CASCA has eigenvector centrality 0.107533935976
PORTIA has eigenvector centrality 0.0895843852913
OCTAVIUS has eigenvector centrality 0.0719553297477
MESSALA has eigenvector centrality 0.0678015660149
TITINIUS has eigenvector centrality 0.0605447822841
SECOND SERVANT has eigenvector centrality 0.0582989519214
LUCILIUS has eigenvector centrality 0.0568690695922
ARTEMIDORUS has eigenvector centrality 0.049707570744
DECIUS has eigenvector centrality 0.046761395152
CINNA has eigenvector centrality 0.0374566923209
CALPURNIA has eigenvector centrality 0.0373371141457
METELLUS has eigenvector centrality 0.0353428391374
PUBLIUS has eigenvector centrality 0.028372767546
THIRD CITIZEN has eigenvector centrality 0.0276832880722
CICERO has eigenvector centrality 0.0270620661214
SECOND CITIZEN has eigenvector centrality 0.0258825853217
FOURTH CITIZEN has eigenvector centrality 0.0239719411169
LIGARIUS has eigenvector centrality 0.0233662957044
PINDARUS has eigenvector centrality 0.0214465181084
TREBONIUS has eigenvector centrality 0.0193717669888
VOLUMNIUS has eigenvector centrality 0.0192196734199
STRATO has eigenvector centrality 0.0172068504017
FIRST CITIZEN has eigenvector centrality 0.0164896325149
POPILIUS has eigenvector centrality 0.0141537963747
CITIZENS has eigenvector centrality 0.0137814910624
CATO has eigenvector centrality 0.0127216694241
CLITUS has eigenvector centrality 0.0116144296158
FLAVIUS has eigenvector centrality 0.0113409246827
LEPIDUS has eigenvector centrality 0.00965320604304
CLAUDIUS has eigenvector centrality 0.00770919475127
SOOTHSAYER has eigenvector centrality 0.00677096048404
SECOND SOLDIER has eigenvector centrality 0.00655207613643
THIRD SERVANT has eigenvector centrality 0.00609362169447
DARDANIUS has eigenvector centrality 0.00407919018453
VARRO has eigenvector centrality 0.00386526006729
GHOST has eigenvector centrality 0.00384393468398
LUCIUS has eigenvector centrality 0.0031585954007
MARULLUS has eigenvector centrality 0.000726029514698
CINNA THE POET has eigenvector centrality 0.000505787351982

PageRank

In [89]:
G = graph_dic[ract_dic[cnum[0]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
pr = nx.pagerank(G)
prs={}
for k,v in pr.items():
    prs[v]=k
for k in sorted(prs,reverse=True):
    print labels[prs[k]], 'has PageRank', k
pos=pos_dict[0]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=pr.keys(),
                       node_size = [d*5000 for d in pr.values()],node_color=pr.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act I PageRank",fontsize=20)
kk=plt.axis('off')
CASSIUS has PageRank 0.251233483301
CAESAR has PageRank 0.201887006098
BRUTUS has PageRank 0.139178732396
CASCA has PageRank 0.133932647701
ANTONIUS has PageRank 0.0544413274707
CINNA has PageRank 0.0429695583521
CICERO has PageRank 0.0367655577096
CALPURNIA has PageRank 0.0230788949999
FLAVIUS has PageRank 0.019829144157
METELLUS has PageRank 0.019810707261
SOOTHSAYER has PageRank 0.0128771680797
MARULLUS has PageRank 0.0128248079976
TITINIUS has PageRank 0.012764598799
In [90]:
G = graph_dic[ract_dic[cnum[1]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
pr = nx.pagerank(G)
prs={}
for k,v in pr.items():
    prs[v]=k
for k in sorted(prs,reverse=True):
    print labels[prs[k]], 'has PageRank', k
pos=pos_dict[1]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=pr.keys(),
                       node_size = [d*5000 for d in pr.values()],node_color=pr.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act II PageRank",fontsize=20)
kk=plt.axis('off')
CAESAR has PageRank 0.2661357823
BRUTUS has PageRank 0.169082553242
CASSIUS has PageRank 0.0969303395982
PORTIA has PageRank 0.0869466229888
ARTEMIDORUS has PageRank 0.0747522889882
DECIUS has PageRank 0.0474025817199
CALPURNIA has PageRank 0.0366272027061
LIGARIUS has PageRank 0.0359563936097
ANTONIUS has PageRank 0.0344289088451
METELLUS has PageRank 0.033758840356
TREBONIUS has PageRank 0.0235508361022
CASCA has PageRank 0.0202241551251
PUBLIUS has PageRank 0.015476891366
LUCIUS has PageRank 0.0131598527747
CATO has PageRank 0.0121827423779
In [91]:
G = graph_dic[ract_dic[cnum[2]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
pr = nx.pagerank(G)
prs={}
for k,v in pr.items():
    prs[v]=k
for k in sorted(prs,reverse=True):
    print labels[prs[k]], 'has PageRank', k
pos=pos_dict[2]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=pr.keys(),
                       node_size = [d*5000 for d in pr.values()],node_color=pr.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act III PageRank",fontsize=20)
kk=plt.axis('off')
CAESAR has PageRank 0.204127949824
ANTONIUS has PageRank 0.195450508218
BRUTUS has PageRank 0.181334385902
CASSIUS has PageRank 0.0692427648015
SECOND SERVANT has PageRank 0.0455461034776
CINNA has PageRank 0.0315547367435
THIRD CITIZEN has PageRank 0.0302966220665
FOURTH CITIZEN has PageRank 0.0212783690624
METELLUS has PageRank 0.0202448772514
DECIUS has PageRank 0.019355241069
CASCA has PageRank 0.0187643398917
SECOND CITIZEN has PageRank 0.0185198417703
PUBLIUS has PageRank 0.0167035354155
FIRST CITIZEN has PageRank 0.0148334261135
OCTAVIUS has PageRank 0.0134256767816
ARTEMIDORUS has PageRank 0.0133319087372
POPILIUS has PageRank 0.0132472448921
CITIZENS has PageRank 0.0131207204554
TREBONIUS has PageRank 0.0125439010613
CINNA THE POET has PageRank 0.0122113939161
THIRD SERVANT has PageRank 0.0097331828424
LIGARIUS has PageRank 0.00882523271623
LEPIDUS has PageRank 0.00828755980732
SOOTHSAYER has PageRank 0.00802047718429
In [92]:
G = graph_dic[ract_dic[cnum[3]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
pr = nx.pagerank(G)
prs={}
for k,v in pr.items():
    prs[v]=k
for k in sorted(prs,reverse=True):
    print labels[prs[k]], 'has PageRank', k
pos=pos_dict[3]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=pr.keys(),
                       node_size = [d*5000 for d in pr.values()],node_color=pr.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act IV PageRank",fontsize=20)
kk=plt.axis('off')
BRUTUS has PageRank 0.287547289346
CASSIUS has PageRank 0.203947332928
ANTONIUS has PageRank 0.0810699579034
MESSALA has PageRank 0.0575814071193
OCTAVIUS has PageRank 0.0528627334901
LEPIDUS has PageRank 0.0465865262798
LUCILIUS has PageRank 0.0410940239182
CLAUDIUS has PageRank 0.029350135649
LUCIUS has PageRank 0.0277578479339
TITINIUS has PageRank 0.0262820235669
PORTIA has PageRank 0.0258076876675
VARRO has PageRank 0.0249855121103
PINDARUS has PageRank 0.0223055399505
CICERO has PageRank 0.0222099869548
CAESAR has PageRank 0.0220005241787
PUBLIUS has PageRank 0.0154233180533
GHOST has PageRank 0.0131881529505
In [93]:
G = graph_dic[ract_dic[cnum[4]]]
G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
pr = nx.pagerank(G)
prs={}
for k,v in pr.items():
    prs[v]=k
for k in sorted(prs,reverse=True):
    print labels[prs[k]], 'has PageRank', k
pos=pos_dict[4]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=pr.keys(),
                       node_size = [d*5000 for d in pr.values()],node_color=pr.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Act V PageRank",fontsize=20)
kk=plt.axis('off')
BRUTUS has PageRank 0.222261740046
ANTONIUS has PageRank 0.102163624162
CASSIUS has PageRank 0.0952176210977
TITINIUS has PageRank 0.0888877510233
MESSALA has PageRank 0.0838174991249
OCTAVIUS has PageRank 0.062006906361
CAESAR has PageRank 0.0529841520393
LUCILIUS has PageRank 0.0529600868806
PINDARUS has PageRank 0.0397820084226
CLITUS has PageRank 0.0363548770631
CATO has PageRank 0.0360605678473
STRATO has PageRank 0.0344715082711
DARDANIUS has PageRank 0.0275896741013
VOLUMNIUS has PageRank 0.0253955278086
SECOND SOLDIER has PageRank 0.01561146317
CASCA has PageRank 0.0122970634906
FLAVIUS has PageRank 0.0121379290911
In [94]:
G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
# G.remove_nodes_from(nx.isolates(G))
plt.figure(figsize=(12,12))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}
pr = nx.pagerank(G)
prs={}
for k,v in pr.items():
    prs[v]=k
for k in sorted(prs,reverse=True):
    print labels[prs[k]], 'has PageRank', k
pos=pos_dict[5]
edgewidth=[]
for (u,v,d) in GI.edges(data=True):
    edgewidth.append(d['weight'])
nx.draw_networkx_nodes(G,pos=pos,with_labels=False,nodelist=pr.keys(),
                       node_size = [d*2000 for d in pr.values()],node_color=pr.values(),
                       cmap=plt.cm.Reds,alpha=0.4)
labe=nx.draw_networkx_labels(G,pos=pos,labels=labels,font_size=20)
nx.draw_networkx_edges(G,pos=pos,edge_color='b',width=edgewidth, alpha=0.5)#,edge_labels=weights,label_pos=.2)
plt.title("Julius Caesar Network (all Acts) PageRank",fontsize=20)
kk=plt.axis('off')
BRUTUS has PageRank 0.198605997325
CAESAR has PageRank 0.154062146007
CASSIUS has PageRank 0.119383059052
ANTONIUS has PageRank 0.102982271431
CASCA has PageRank 0.0328495727695
MESSALA has PageRank 0.0247701176677
TITINIUS has PageRank 0.0222825875196
OCTAVIUS has PageRank 0.0208526824097
PORTIA has PageRank 0.0190254060854
CINNA has PageRank 0.0185694963756
ARTEMIDORUS has PageRank 0.0167514935994
LUCILIUS has PageRank 0.0161728804352
SECOND SERVANT has PageRank 0.0148287203774
DECIUS has PageRank 0.0144036384226
METELLUS has PageRank 0.0138111613683
PINDARUS has PageRank 0.0114506142195
CLITUS has PageRank 0.010782700724
CICERO has PageRank 0.0107015466583
CALPURNIA has PageRank 0.0104218440352
CATO has PageRank 0.00967994650853
THIRD CITIZEN has PageRank 0.00959101644611
LIGARIUS has PageRank 0.00923337979009
PUBLIUS has PageRank 0.00905621792457
LUCIUS has PageRank 0.00888283565862
DARDANIUS has PageRank 0.00866276257946
STRATO has PageRank 0.0084014066605
FOURTH CITIZEN has PageRank 0.00834578958314
TREBONIUS has PageRank 0.00820503868397
SECOND CITIZEN has PageRank 0.00802292823669
LEPIDUS has PageRank 0.00793969246512
CLAUDIUS has PageRank 0.0071853216538
VARRO has PageRank 0.00659508964882
VOLUMNIUS has PageRank 0.00643953211795
FIRST CITIZEN has PageRank 0.00635796578411
FLAVIUS has PageRank 0.00586170179384
CITIZENS has PageRank 0.00579684395147
POPILIUS has PageRank 0.00579025852887
CINNA THE POET has PageRank 0.00514978364942
SECOND SOLDIER has PageRank 0.00463972593065
SOOTHSAYER has PageRank 0.0046221441247
THIRD SERVANT has PageRank 0.0046163799415
MARULLUS has PageRank 0.00413769775718
GHOST has PageRank 0.00407860409801

Communities

In [121]:
from chAs import draw_comms,modul_arity
import community as comm

G = graph_dic[ract_dic[cnum[0]]]
G.remove_nodes_from(nx.isolates(G))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}

part=comm.best_partition(G) 
print 'Number of communities of Julius Caesar Act I network =', max(part.values())+1
print 'Community partition of Julius Caesar Act I network:'
parLis=[]
partdi={}
for i,k in part.items():
    if k not in partdi:
        partdi[k]=[nodper_dic[i]]
    else:
        partdi[k].append(nodper_dic[i])
for i,k in partdi.items():
    parLis.append(k)
print parLis
print 'Community modularity of Julius Caesar Act I network =', "%.4f" % comm.modularity(part,G)

d=0.8 
dd=0.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
sstt="The %s Communities of Julius Caesar Act I Network" %(max(part.values())+1)

draw_comms(G,G.nodes(),[],[],[] ,part,part,d,dd,c,cc,alpha,ealpha,nodper_dic,sstt)
Number of communities of Julius Caesar Act I network = 2
Community partition of Julius Caesar Act I network:
[['MARULLUS', 'CASCA', 'CICERO', 'CAESAR', 'CALPURNIA', 'FLAVIUS', 'SECOND CITIZEN', 'ANTONIUS', 'SOOTHSAYER'], ['DECIUS', 'BRUTUS', 'TITINIUS', 'CASSIUS', 'METELLUS', 'CINNA', 'TREBONIUS']]
Community modularity of Julius Caesar Act I network = 0.1581
<matplotlib.figure.Figure at 0x108e67450>
In [122]:
from chAs import draw_comms,modul_arity
import community as comm

G = graph_dic[ract_dic[cnum[1]]]
G.remove_nodes_from(nx.isolates(G))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}

part=comm.best_partition(G) 
print 'Number of communities of Julius Caesar Act II network =', max(part.values())+1
print 'Community partition of Julius Caesar Act II network:'
parLis=[]
partdi={}
for i,k in part.items():
    if k not in partdi:
        partdi[k]=[nodper_dic[i]]
    else:
        partdi[k].append(nodper_dic[i])
for i,k in partdi.items():
    parLis.append(k)
print parLis
print 'Community modularity of Julius Caesar Act II network =', "%.4f" % comm.modularity(part,G)

d=0.8 
dd=0.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
sstt="The %s Communities of Julius Caesar Act II Network" %(max(part.values())+1)

draw_comms(G,G.nodes(),[],[],[] ,part,part,d,dd,c,cc,alpha,ealpha,nodper_dic,sstt)
Number of communities of Julius Caesar Act II network = 3
Community partition of Julius Caesar Act II network:
[['PORTIA', 'BRUTUS', 'LIGARIUS', 'METELLUS', 'CATO'], ['CASCA', 'CICERO', 'LUCIUS', 'CASSIUS', 'CINNA', 'ANTONIUS'], ['DECIUS', 'CAESAR', 'CALPURNIA', 'PUBLIUS', 'ARTEMIDORUS', 'TREBONIUS']]
Community modularity of Julius Caesar Act II network = 0.2184
<matplotlib.figure.Figure at 0x107a87150>
In [123]:
from chAs import draw_comms,modul_arity
import community as comm

G = graph_dic[ract_dic[cnum[2]]]
G.remove_nodes_from(nx.isolates(G))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}

part=comm.best_partition(G) 
print 'Number of communities of Julius Caesar Act III network =', max(part.values())+1
print 'Community partition of Julius Caesar Act III network:'
parLis=[]
partdi={}
for i,k in part.items():
    if k not in partdi:
        partdi[k]=[nodper_dic[i]]
    else:
        partdi[k].append(nodper_dic[i])
for i,k in partdi.items():
    parLis.append(k)
print parLis
print 'Community modularity of Julius Caesar Act III network =', "%.4f" % comm.modularity(part,G)

d=0.8 
dd=0.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
sstt="The %s Communities of Julius Caesar Act III Network" %(max(part.values())+1)

draw_comms(G,G.nodes(),[],[],[] ,part,part,d,dd,c,cc,alpha,ealpha,nodper_dic,sstt)
Number of communities of Julius Caesar Act III network = 3
Community partition of Julius Caesar Act III network:
[['FIRST CITIZEN', 'SECOND SERVANT', 'FOURTH CITIZEN', 'BRUTUS', 'CAESAR', 'THIRD SERVANT', 'LEPIDUS', 'OCTAVIUS', 'SECOND CITIZEN', 'ANTONIUS', 'ARTEMIDORUS', 'CITIZENS', 'SOOTHSAYER'], ['CINNA THE POET', 'CASCA', 'LIGARIUS', 'THIRD CITIZEN', 'CINNA'], ['DECIUS', 'CASSIUS', 'PUBLIUS', 'METELLUS', 'TREBONIUS', 'POPILIUS']]
Community modularity of Julius Caesar Act III network = 0.0891
<matplotlib.figure.Figure at 0x10b25c990>
In [124]:
from chAs import draw_comms,modul_arity
import community as comm

G = graph_dic[ract_dic[cnum[3]]]
G.remove_nodes_from(nx.isolates(G))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}

part=comm.best_partition(G) 
print 'Number of communities of Julius Caesar Act IV network =', max(part.values())+1
print 'Community partition of Julius Caesar Act IV network:'
parLis=[]
partdi={}
for i,k in part.items():
    if k not in partdi:
        partdi[k]=[nodper_dic[i]]
    else:
        partdi[k].append(nodper_dic[i])
for i,k in partdi.items():
    parLis.append(k)
print parLis
print 'Community modularity of Julius Caesar Act IV network =', "%.4f" % comm.modularity(part,G)

d=0.8 
dd=0.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
sstt="The %s Communities of Julius Caesar Act IV Network" %(max(part.values())+1)

draw_comms(G,G.nodes(),[],[],[] ,part,part,d,dd,c,cc,alpha,ealpha,nodper_dic,sstt)
Number of communities of Julius Caesar Act IV network = 3
Community partition of Julius Caesar Act IV network:
[['PORTIA', 'BRUTUS', 'TITINIUS', 'PINDARUS', 'LUCILIUS', 'GHOST', 'CASSIUS'], ['CLAUDIUS', 'VARRO', 'LUCIUS'], ['MESSALA', 'CICERO', 'CAESAR', 'LEPIDUS', 'PUBLIUS', 'OCTAVIUS', 'ANTONIUS']]
Community modularity of Julius Caesar Act IV network = 0.2031
<matplotlib.figure.Figure at 0x10bee8550>
In [125]:
from chAs import draw_comms,modul_arity
import community as comm

G = graph_dic[ract_dic[cnum[4]]]
G.remove_nodes_from(nx.isolates(G))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}

part=comm.best_partition(G) 
print 'Number of communities of Julius Caesar Act V network =', max(part.values())+1
print 'Community partition of Julius Caesar Act V network:'
parLis=[]
partdi={}
for i,k in part.items():
    if k not in partdi:
        partdi[k]=[nodper_dic[i]]
    else:
        partdi[k].append(nodper_dic[i])
for i,k in partdi.items():
    parLis.append(k)
print parLis
print 'Community modularity of Julius Caesar Act V network =', "%.4f" % comm.modularity(part,G)

d=0.8 
dd=0.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
sstt="The %s Communities of Julius Caesar Act V Network" %(max(part.values())+1)

draw_comms(G,G.nodes(),[],[],[] ,part,part,d,dd,c,cc,alpha,ealpha,nodper_dic,sstt)
Number of communities of Julius Caesar Act V network = 3
Community partition of Julius Caesar Act V network:
[['CLITUS', 'BRUTUS', 'LUCILIUS', 'DARDANIUS', 'VOLUMNIUS', 'FLAVIUS', 'CATO'], ['CASCA', 'CAESAR', 'OCTAVIUS', 'SECOND SOLDIER', 'ANTONIUS'], ['MESSALA', 'TITINIUS', 'PINDARUS', 'STRATO', 'CASSIUS']]
Community modularity of Julius Caesar Act V network = 0.2865
<matplotlib.figure.Figure at 0x1029de850>
In [126]:
from chAs import draw_comms,modul_arity
import community as comm

G=plot_total_graph_with_weights(graph_dic,nodper_dic)
weights={(nd[0],nd[1]):str(nd[2]['weight']) for nd in G.edges(data=True)}
# G.remove_nodes_from(nx.isolates(G))
# G = graph_dic[ract_dic[cnum[0]]]
# G.remove_nodes_from(nx.isolates(G))
labels={i:v for v,i in pernode_dict.items() if i in G.nodes()}

part=comm.best_partition(G) 
print 'Number of communities of Julius Caesar network (all Acts) =', max(part.values())+1
print 'Community partition of Julius Caesar network (all Acts):'
parLis=[]
partdi={}
for i,k in part.items():
    if k not in partdi:
        partdi[k]=[nodper_dic[i]]
    else:
        partdi[k].append(nodper_dic[i])
for i,k in partdi.items():
    parLis.append(k)
print parLis
print 'Community modularity of Julius Caesar network (all Acts) =', "%.4f" % comm.modularity(part,G)

d=0.8 
dd=0.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
sstt="The %s Communities of Julius Caesar Network (all Acts)" %(max(part.values())+1)

draw_comms(G,G.nodes(),[],[],[] ,part,part,d,dd,c,cc,alpha,ealpha,nodper_dic,sstt)
Number of communities of Julius Caesar network (all Acts) = 5
Community partition of Julius Caesar network (all Acts):
[['PORTIA', 'CLAUDIUS', 'VARRO', 'MESSALA', 'BRUTUS', 'TITINIUS', 'LIGARIUS', 'LUCIUS', 'PINDARUS', 'LUCILIUS', 'GHOST', 'STRATO', 'VOLUMNIUS', 'CASSIUS', 'PUBLIUS', 'CATO', 'POPILIUS'], ['FIRST CITIZEN', 'SECOND SERVANT', 'DECIUS', 'CAESAR', 'THIRD SERVANT', 'CALPURNIA', 'LEPIDUS', 'OCTAVIUS', 'SECOND SOLDIER', 'METELLUS', 'SECOND CITIZEN', 'ANTONIUS', 'ARTEMIDORUS', 'CITIZENS', 'TREBONIUS', 'SOOTHSAYER'], ['MARULLUS', 'CASCA', 'CICERO', 'FLAVIUS', 'THIRD CITIZEN'], ['CLITUS', 'DARDANIUS'], ['CINNA THE POET', 'FOURTH CITIZEN', 'CINNA']]
Community modularity of Julius Caesar network (all Acts) = 0.1801
<matplotlib.figure.Figure at 0x107dcc6d0>
In [ ]: